package com.yun.algorithm.sort;

import java.util.ArrayList;
import java.util.Arrays;

//基数排序 最低有效数字
public class RadixSort {
    public static void radixSort(String[] a, int length) {
        ArrayList<String>[] buckets = new ArrayList[10];
        for (int i = 0; i < buckets.length; i++) {
            buckets[i] = new ArrayList<>();
        }
        for (int i = length-1; i >=0 ; i--) {
            //'0'->48 '1'->49
            for (String s : a) {
                buckets[s.charAt(i)-'0'].add(s);
            }
            int k=0;
            for (ArrayList<String> bucket : buckets) {
                //System.out.println(bucket);
                for (String s : bucket) {
                    a[k++]=s;
                }
                bucket.clear();
            }
            System.out.println(Arrays.toString(a));
        }
    }

    public static void main(String[] args) {
        String[] phoneNumbers=new String[6];
        phoneNumbers[0]="158";
        phoneNumbers[1]="151";
        phoneNumbers[2]="135";
        phoneNumbers[3]="138";
        phoneNumbers[4]="139";
        phoneNumbers[5]="159";

        RadixSort.radixSort(phoneNumbers,3);
    }
}
